﻿using BBCore.Core.Models.Admins;
using BootstrapBlazor.Components;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Routing;
using System.Security.Claims;
using System.Security.Principal;
using Console = System.Console;

namespace BBCore.Server.Components
{
    public partial class Routes
    {


        #region 每个页面都拦截验证，太耗资源了
        /// <summary>
        /// 白名单
        /// </summary>
        //private List<string> whiteUrl = new List<string>()
        //{
        //"/Login",
        //"/NotAuth",
        //"/api/Account/Logout"
        //};
        //private async Task OnNavigateAsync(NavigationContext context)
        //{
        //    var path = NavigationManager.ToAbsoluteUri(context.Path).LocalPath;
        //    Console.WriteLine("测试地址:"+path);
        //    if (whiteUrl.Any(x => x == path))
        //    {

        //    }
        //    else
        //    {
        //        var user = (await AuthenticationStateProvider.GetAuthenticationStateAsync()).User;
        //        if (user == null)
        //        {
        //            NavigationManager.NavigateTo("/Login");
        //            return;
        //        }

        //        if (user.Identity?.IsAuthenticated == false)
        //        {
        //            NavigationManager.NavigateTo("/Login");
        //            return;
        //        }

        //        var roleId = user.FindFirst(ClaimTypes.Role)?.Value;
        //        if (roleId == null)
        //        {
        //            NavigationManager.NavigateTo("/NotAuth");
        //            return;
        //        }

        //        var role = await RoleEntity.Where(x => x.Id == int.Parse(roleId)).IncludeMany(x => x.Menus).FirstAsync();
        //        Console.WriteLine("老子看你");
        //        if (role == null || role.Menus == null)
        //        {
        //            NavigationManager.NavigateTo("/NotAuth");
        //            return;
        //        }

        //        var authUrl = role.Menus.Select(x => x.Url!).ToList();

        //        if (authUrl.Any(x => x == path))
        //        {
        //            return;
        //        }
        //        NavigationManager.NavigateTo("/NotAuth");
        //    }
        //}
        #endregion


        /// <summary>
        /// 需要拦截验证的URL
        /// </summary>
        private List<string> whiteUrl = new List<string>()
        {            
         "/", //首页
         "/user",//用户管理
         "/role",//角色管理
         "/menu",//菜单管理
         "/dispatch", //消息通知
         "/AnnouncementPage" //公告栏管理

        };
        private async Task OnNavigateAsync(NavigationContext context)
        {
            var path = NavigationManager.ToAbsoluteUri(context.Path).LocalPath;

            if (whiteUrl.Any(x => x == path))
            {
                var user = (await AuthenticationStateProvider.GetAuthenticationStateAsync()).User;
                //用户未登录
                if (user == null)
                {
                    NavigationManager.NavigateTo("/Login");
                    return;
                }
                //用户没权限
                if (user.Identity?.IsAuthenticated == false)
                {
                    NavigationManager.NavigateTo("/Login");
                    return;
                }
                //获取角色ID
                var roleId = user.FindFirst(ClaimTypes.Role)?.Value;
                if (roleId == null)
                {
                    //跳转到没权限页面
                    NavigationManager.NavigateTo("/NotAuth");
                    return;
                }
                //获取角色及下挂的菜单
                var role = RoleEntity.Where(x => x.Id == int.Parse(roleId)).IncludeMany(x => x.Menus).First();
                if (role == null || role.Menus == null)
                {
                    NavigationManager.NavigateTo("/NotAuth");
                    return;
                }
                //角色下挂的菜单列表
                var authUrl = role.Menus.Select(x => x.Url!).ToList();
                //角色下挂的菜单列表是否包含需要拦截验证的URL
                if (authUrl.Any(x => x == path))
                {
                    return;
                }
                NavigationManager.NavigateTo("/NotAuth");
            }
            else
            {
                return;
            }
        }
    }
}
